package com.kfm.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.kfm.hotel.domain.IntakeInfo;
import com.kfm.hotel.mapper.IntakeInfoMapper;
import com.kfm.hotel.service.IntakeInfoService;
import com.kfm.hotel.service.RoomService;
import com.kfm.hotel.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author 10854
 * @description 针对表【intake_info】的数据库操作Service实现
 * @createDate 2023-06-09 09:07:52
 */
@Service
public class IntakeInfoServiceImpl extends MPJBaseServiceImpl<IntakeInfoMapper, IntakeInfo>
        implements IntakeInfoService {

    @Autowired
    private RoomService roomService;

    @Override
    @Transactional
    public void checkIn(IntakeInfo intakeInfo) {
        save(intakeInfo);
        roomService.updateState(intakeInfo.getRoomId(), Constant.ROOM_STATE_CHECKIN);
    }

    @Override
    public PageInfo<IntakeInfo> listCheckOut(IntakeInfo entity, Integer page, Integer limit) {
        QueryWrapper<IntakeInfo> wrapper = new QueryWrapper<>();
        wrapper.isNull("exit_time");
        wrapper.eq("is_del", 0);

        PageHelper.startPage(page, limit);
        List<IntakeInfo> list = listDeep(wrapper);
        return PageInfo.of(list);
    }

    @Override
    @Transactional
    public void checkOut(IntakeInfo intakeInfo, Integer opertorId) {
        intakeInfo.setExitTime(new Date());
        intakeInfo.setOperator(opertorId);
        intakeInfo.setUpdator(opertorId);
        // 更新入住信息
        updateById(intakeInfo);
        // 更新房间状态
        roomService.updateState(intakeInfo.getRoomId(), Constant.ROOM_STATE_TO_CLEAN);
    }

    @Override
    public List<Long> getIncome() {
        return getBaseMapper().getIncome();
    }


    public Map<String, List> getIncomeByDate() {
        List<Map<String, Object>> result = getBaseMapper().getIncomeByDate();

        Map<String, List> map = new HashMap<>();
        List<String> dateList = new ArrayList<>();
        List<Long> incomeList = new ArrayList<>();
        for (Map<String, Object> m : result){
            dateList.add(m.get("date").toString());
            incomeList.add(Long.parseLong(m.get("total").toString()));
        }
        map.put("date", dateList);
        map.put("income", incomeList);
        return map;
    }
}




